# -*- coding: utf-8 -*-,
# @File : 2.协程基础.py,
# @Author : lidong,
# @IDEA: PyCharm
# @Time :  2022/5/11 15:07

import time
import asyncio


async def get_request(url):
    print('正在请求的url:', url)
    time.sleep(3)
    print('请求结束:', url)
    return 'bobo'

# 参数t为回调函数的调用者
def task_callback(t):
    print('i am task_callback(), 参数t:', t)

if __name__ == '__main__':
    # 返回协程对象
    c = get_request('www.2.com')
    # 封装为任务对象
    task = asyncio.ensure_future(c)
    # 绑定回调函数
    task.add_done_callback(task_callback)
    # 创建事件循环
    loop = asyncio.get_event_loop()
    # 将任务对象注册到事件循环且开启事件循环
    loop.run_until_complete(task)